/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>Project: app2022 - Ex7
 * <p>Powered by webrx On 2022-02-08 11:47:39
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex7 {
    AtomicInteger n = new AtomicInteger(0);
    ReentrantLock lock = new ReentrantLock(true);

    public static void main(String[] args) {
        var t = new Ex7();
        new Thread(t::work, "A").start();
        new Thread(t::work, "B").start();
        new Thread(t::work, "C").start();
    }

    void work() {
        var t = Thread.currentThread().getName();
        while (true) {
            lock.lock();
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.printf("%s%d ", t, n.incrementAndGet());
            if ("C".equalsIgnoreCase(t)) {
                System.out.println();
            }
            lock.unlock();
        }

    }
}
